#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   http_ip.sh
# Version:    1.0
# Date:       2013/11/08
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-08Apache-01字符模式-06访问日志
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/11/12
#             - access log.
#             Version 1.1, 2015/01/15
#             - add cleanup killall firefox
# ----------------------------------------------------------------------

##! @TODO: setup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    serv=httpd

    server_ip=`ifconfig -a | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`
    if [ -z ${server_ip} ]; then
        echo "failed: get local ip null, pls check"
        exit 1
    fi


    # restart
    restart_daemon ${serv}

    sleep 3
    # check httpd restart
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} restart"

    trap do_cleanup INT EXIT

}

##! @TODO: test
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    if exists firefox
    then
        export DISPLAY=:0
        firefox ${server_ip} &
        sleep 10
    fi 


    # check log
    HTTPD_LOG=/var/log/httpd/access_log
    [ -f ${HTTPD_LOG} ] && tail -1 ${HTTPD_LOG} | grep ${server_ip}
    EchoResult "httpd: access log"

}

##! @TODO: cleanup
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_cleanup()
{
    stop_daemon ${serv}

}

# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh



# main 
do_setup
do_test
EchoResult "testcase : httpd access log"
